package com.atguigu.daijia.dispatch.client.fallback;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.model.vo.dispatch.NewOrderTaskVo;
import com.atguigu.daijia.model.vo.order.NewOrderDataVo;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @create: 2024/9/25
 * @author: gyhe10
 * @description: NewOrderFeignClient 接口的降级实现工厂类
 **/

@Component
public class NewOrderFeignClientFallbackFactory implements FallbackFactory<NewOrderFeignClient> {
    @Override
    public NewOrderFeignClient create(Throwable cause) {
        return new NewOrderFeignClient() {
            @Override
            public Result<Long> addAndStartTask(NewOrderTaskVo newOrderTaskVo) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<List<NewOrderDataVo>> findNewOrderQueueData(Long driverId) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }

            @Override
            public Result<Boolean> clearNewOrderQueueData(Long driverId) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }
        };
    }
}
